<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <el-select
            v-model="condition.status"
            clearable
            placeholder="账户状态"
            style="width: 150px"
            :empty-values="[null, undefined]"
            :value-on-clear="null"
            @change="getUserList(1)"
        >
          <el-option label="可用" :value="1"/>
          <el-option label="不可用" :value="0"/>
        </el-select>
      </div>
    </template>
    <el-table :data="userList.list" style="width: 100%">
      <el-table-column prop="id" label="id"/>
      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="phone" label="手机号"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column prop="realname" label="姓名"/>
      <el-table-column prop="money" label="余额"/>
      <el-table-column prop="regTime" label="注册时间"/>
      <el-table-column label="状态">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              inline-prompt
              :active-value=1
              active-text="正常"
              :inactive-value=0
              inactive-text="禁用"
              @change="(status) => {updateStatusInfo(scope.row.id,status)}"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="showEdit(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-row class="row-bg" justify="space-evenly">
        <el-col :span="6">
          <el-pagination layout="prev, pager, next" :total="userList.total" :page-size="userList.pageSize"
                         @current-change="getUserList"/>
        </el-col>
      </el-row>
    </template>
  </el-card>


  <!-- START 修改对话框 -->
  <el-dialog v-model="updateVisible" title="编辑管理员信息" align-center style="width: 300px">
    <el-form :model="updateInfo">
      <el-form-item label="手机号" style="width: 250px">
        <el-input v-model="updateInfo.phone" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="邮箱" style="width: 250px">
        <el-input v-model="updateInfo.email" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="姓名" style="width: 250px">
        <el-input v-model="updateInfo.realname" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="余额" style="width: 250px">
        <el-input v-model="updateInfo.money" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateVisible = false">取消</el-button>
        <el-button type="primary" @click="subUpdate">提交</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- END 修改对话框 -->

</template>

<script setup>
import {ref} from "vue";
import adminApi from "@/api/AdminApi.js";
import {ElMessage} from "element-plus";
import UserApi from "@/api/UserApi.js";

const userList = ref({
  pageNum: 1,
  pageSize: 5,
  total: 10,
  list: []
});

const updateVisible = ref(false)
const updateInfo = ref({})

// 搜索条件相关信息
const condition = ref({
  id:null,
  status:null
})

// 获得全部管理员
const getUserList = (pageNum) => {
  UserApi.getALl( condition.value, pageNum, 5)
      .then(resp => {
        if (resp.code === 10000) {
          userList.value = resp.data
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 修改用户状态信息
const updateStatusInfo = (id, status) => {
  let date = {id, status}
  UserApi.updateInfo(date)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
          getUserList(userList.value.pageNum)
        } else {
          ElMessage.error(resp.msg)
        }
      })
}

// 打开编辑弹出层
const showEdit = (id) => {
  UserApi.getById(id)
      .then(resp => {
        if (resp.code === 10000) {
          updateInfo.value = resp.data
          updateVisible.value = true
        } else {
          ElMessage.error(resp.msg)
        }
      })
}


// 提交更新
const subUpdate = () => {
  UserApi.updateInfo(updateInfo.value)
      .then(resp => {
        if (resp.code === 10000) {
          ElMessage.success(resp.msg)
          getUserList(userList.value.pageNum)
          updateVisible.value = false
        } else {
          ElMessage.error(resp.msg)
        }
      })
}



getUserList(1)
</script>

<style scoped>

</style>